package com.yj.cloudnote.controller;

import com.yj.cloudnote.model.Pad;
import com.yj.cloudnote.service.PadService;
import net.gjerull.etherpad.client.EPLiteClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: jy
 * @Description:
 * @Date: 16:14 2018/5/20
 */
@Controller
@RequestMapping(value = "/pad")
public class PadController {

    @Autowired
    private PadService padService;
    /**
     * @Author: jy
     * @Description: 新建pad
     * @Date: 16:16 2018/5/20
     * key:5e27c303d08f3c18924032b6f2104ec6e97076b1d3c97c13502e9b126d5e8306
     */
    @RequestMapping(value = "/getNewPad")
    @ResponseBody
    public Map<String,Object> getNewPad(@RequestBody Map<String,Object> params) {
        String username = (String) params.get("username");
        String newPadName = (String) params.get("newPadName");
        EPLiteClient client = new EPLiteClient("http://localhost:9001",
                "5e27c303d08f3c18924032b6f2104ec6e97076b1d3c97c13502e9b126d5e8306");
        // Create pad and set text
        String padUrl = username+"@"+newPadName;
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            client.createPad(padUrl);
            Pad pad = new Pad();
            pad.setUsername(username);
            pad.setPadname(newPadName);
            pad.setUrl("http://localhost:9001/p/"+padUrl);
            padService.insertNewPad(pad);
            client.setText(username+"@"+newPadName, "从这里开始协作编辑");
            result.put("code",0);
        } catch (Exception e) {
            result.put("code",1);
        } finally {

        }
        // Get pad text
//        String text = client.getText("my_pad").get("text").toString();
        // Get list of all pad ids
//        Map result = client.listAllPads();
//        List padIds = (List) result.get("padIDs");
        return result;
    }

    /**
     * @Author: jy
     * @Description:
     * @Date: 21:06 2018/5/26
     */
    @RequestMapping(value = "/getPads")
    @ResponseBody
    public Map<String,Object> getPads(@RequestBody Map<String,Object> params) {
        String username = (String) params.get("username");
        List<Pad> pads = padService.selectByUsername(username);
        Map<String,Object> res = new HashMap<String,Object>();
        res.put("pads",pads);
        return res;
    }

    @RequestMapping(value = "/deletePad")
    @ResponseBody
    public Map<String,Object> deletePad(@RequestBody Map<String,Object> params) {
        int padid = (Integer) params.get("padid");
        String  username = (String ) params.get("username");
        String  padname = (String ) params.get("padname");
        EPLiteClient client = new EPLiteClient("http://localhost:9001",
                "5e27c303d08f3c18924032b6f2104ec6e97076b1d3c97c13502e9b126d5e8306");
        try {
            client.deletePad(username+"@"+padname);
        } catch (Exception e) {

        }
        Map<String,Object> res = new HashMap<String,Object>();
        padService.deleteById(padid);
        return res;
    }
}
